//
//  LLComposeToolbar.m
//  Weibo
//
//  Created by 李灵斌 on 15-2-6.
//  Copyright (c) 2015年 apple. All rights reserved.
//

#import "LLComposeToolbar.h"

@implementation LLComposeToolbar

- (instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        [self setup];
        
    }
    return self;
}

- (id)initWithCoder:(NSCoder *)aDecoder{
    
    if (self = [super initWithCoder:aDecoder]) {
        [self setup];
    }
    return self;
}

- (void)setup
{
    self.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"compose_image_placeholder"]];
    // 1.创建5个按钮
    // 添加所有的子控件
    [self addButtonWithIcon:@"compose_camerabutton_background" highIcon:@"compose_camerabutton_background_highlighted" tag:LLComposeToolbarButtonTypeCamera];
    [self addButtonWithIcon:@"compose_toolbar_picture" highIcon:@"compose_toolbar_picture_highlighted" tag:LLComposeToolbarButtonTypePicture];
    [self addButtonWithIcon:@"compose_mentionbutton_background" highIcon:@"compose_mentionbutton_background_highlighted" tag:LLComposeToolbarButtonTypeMention];
    [self addButtonWithIcon:@"compose_trendbutton_background" highIcon:@"compose_trendbutton_background_highlighted" tag:LLComposeToolbarButtonTypeTrend  ];
    [self addButtonWithIcon:@"compose_emoticonbutton_background" highIcon:@"compose_emoticonbutton_background_highlighted" tag:LLComposeToolbarButtonTypeEmotion];
}
/**
 * 创建一个按钮
 */
- (void)addButtonWithIcon:(NSString *)icon highIcon:(NSString *)highIcon tag:(LLComposeToolbarType)tag{
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:btn];
    btn.tag = tag;
    [btn setImage:[UIImage imageNamed:icon] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:highIcon] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
}
- (void)btnClick:(UIButton *)btn{
    
    if ([self.delegate respondsToSelector:@selector(composeToobarDidClickBtn:type:)]) {
        [self.delegate composeToobarDidClickBtn:self type:(int)btn.tag];
    }
}

- (void)layoutSubviews{
    
    [super layoutSubviews];
    NSUInteger count = self.subviews.count;
    CGFloat width = self.width / count;
    CGFloat heigth = self.height;
    for (int i = 0; i<count; i++) {
        
        UIButton *btn = self.subviews[i];
        btn.width = width;
        btn.height = heigth;
        btn.x = i * width;
    }
}


@end
